EM = '空位'
WR = '白车'
WB = '白象'
WN = '白马'
WQ = '白后'
WK = '白王'
WP = '白兵'
BR = '黑车'
BB = '黑象'
BN = '黑马'
BQ = '黑后'
BK = '黑王'
BP = '黑兵'
board = [
    [WR, WN, WB, WQ, WK, WB, WN, WR],
    [WP, WP, WP, WP, WP, WP, WP, WP],
    [EM, EM, EM, EM, EM, EM, EM, EM],
    [EM, EM, EM, EM, EM, EM, EM, EM],
    [EM, EM, EM, EM, EM, EM, EM, EM],
    [EM, EM, EM, EM, EM, EM, EM, EM],
    [BP, BP, BP, BP, BP, BP, BP, BP],
    [BR, BN, BB, BQ, BK, BB, BN, BR]
]
en = enumerate

def sign(a):
    if a==0:
        return 0
    return a // abs(a)
def out():
    print("\t\tA\t\tB\t\tC\t\tD\t\tE\t\tF\t\tG\t\tH")
    for ii, iv in en(board[::-1]):
        print(8 - ii, end="\t\t")
        for ji, jv in en(iv):
            print(jv, end="\t\t")
        print()


def board_Get(st):
    return board[ord(st[1]) - ord("1")][ord(st[0]) - ord("A")]


def board_Set(st, vlu):
    global board
    board[ord(st[1]) - ord("1")][ord(st[0]) - ord("A")] = vlu


bg = board_Get
bs = board_Set


class Player:
    def __init__(self, name, op_king, self_all):
        self.name = name
        self.op_king = op_king
        self.all = self_all

    def find(self):
        for i in board:
            for j in i:
                if j == self.op_king:
                    return
        print(self.name, "赢了")
        exit()

    def go(self):
        print(self.name,"的回合")
        out()
        left = input("起始位置？")
        right = input("终止位置？")
        if bg(left) not in self.all:
            print("不是你的棋子，别乱走")
            self.go()
        elif bg(right) in self.all:
            print("别对着友军开炮啊")
            self.go()
        else:
            posx = abs(ord(left[0]) - ord(right[0]))
            posy = abs(ord(left[1]) - ord(right[1]))
            pos0 = max(posx, posy)
            pos1 = min(posx, posy)
            if bg(left) in [WN, BN]:
                if pos0 == 2 and pos1 == 1:
                    print("移动成功")
                    bs(right, board_Get(left))
                    bs(left, EM)
                else:
                    print("移动错误。马：跳L字，走通吃")
                    self.go()
            elif bg(left) in [WK, BK]:
                if pos0 == 1:
                    print("移动成功")
                    bs(right, bg(left))
                    bs(left, EM)
                else:
                    print("移动错误。王：八方一格")
                    self.go()
            elif bg(left) in [WB, BB]:
                if pos0 == pos1 and pos0:
                    posix = sign(ord(left[0]) - ord(right[0]))
                    posiy = sign(ord(left[1]) - ord(right[1]))
                    for i in range(1, pos0):
                        if board[ord(left[1]) - ord("1") + posix * i][ord(left[0]) - ord("A") + posiy * i] != EM:
                            print("中间有棋子阻隔")
                            self.go()
                            break
                    else:
                        bs(right, bg(left))
                        bs(left, EM)
                else:
                    print("移动错误。象：斜走不限")
            elif bg(left) in [WR, BR]:
                if pos1 == 0 and pos0:
                    posix = sign(ord(left[0]) - ord(right[0]))
                    posiy = sign(ord(left[1]) - ord(right[1]))
                    posix , posiy = posiy , posix
                    for i in range(1, pos0):
                        if board[ord(left[1]) - ord("1") + posix * i][ord(left[0]) - ord("A") + posiy * i] != EM:
                            print("中间有棋子阻隔")
                            self.go()
                            break
                    else:
                        bs(right, bg(left))
                        bs(left, EM)
                    pass  # TODO


if __name__ == '__main__':
    W = Player("白棋", BK, [WK, WQ, WR, WN, WP, WB])
    B = Player("黑棋", WK, [BK, BQ, BR, BN, BP, BB])
    while True:
        W.find()
        W.go()
        B.find()
        B.go()
